<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor :itemList="[
        { name: 'box-base', title: '基础信息' },
        { name: 'box-contact', title: '发货联系信息' },
        { name: 'box-invoice', title: '发票抬头' },
        { name: 'box-list', title: '厂商管理' }]" />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customSpace v-if="true == propIsInfo && utils.isSp()">
        <template #content>
          <customButton
            theme="primary"
            title="保存"
            @click="saveInfo"
          >
          </customButton>
        </template>
      </customSpace>
      <customForm :data="form" :rules="cpPropRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">基础信息</div>
          <customFormItem
            label="供应商名称" name="name" :parent="_self" :form="form"
            :disabled="strings.isNotBlank(form.id)"
          />
          <customFormItem label="统一社会信用码" name="companyCode" :parent="_self" :form="form" />

          <customFormItem  label=" 登录账号" name="loginKey"
                           :disabled="strings.isNotBlank(form.id)"
                           :parent="_self" :form="form" help="不指定则自动生成"/>
          <customFormItem label="识别码" name="code"
                          :parent="_self" :form="form"
                          :config="{ defaultValue: '自动获取'}"
                          disabled />

          <customFormItem label="助记码" name="pinyin" :parent="_self" :form="form" />
          <customFormItem
            label="供货类型"
            name="spType"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.checkbox.name"
            :config="{options: constCommon.busConst.matrType}"
            fullWidth
          >
          </customFormItem>

          <div id="box-contact" class="box-title">发货联系信息</div>
          <customFormItem label="联系人" name="contactor" :parent="_self" :form="form" />
          <customFormItem label="手机号" name="mobile" :parent="_self" :form="form" />
          <customFormItem label="座机号码" name="phone" :parent="_self" :form="form" />
          <customFormItem label="邮箱" name="email" :parent="_self" :form="form" />
          <customFormItem label="联系地址" name="address"
                          :parent="_self"
                          :form="form"
                          fullWidth
                          heightAuto
                          :type="constCommon.formItemType.textarea.name"
                          :config="{maxlength: 200, minRows: 1, maxRows: 5}" />

          <div id="box-invoice" class="box-title">发票抬头</div>
          <customFormItem label="名称" name="invoiceName" :parent="_self" :form="form" />
          <customFormItem label="税号" name="invoiceNo" :parent="_self" :form="form" />
          <customFormItem label="单位地址" name="invoiceAddress" :parent="_self" :form="form" />
          <customFormItem label="电话号码" name="invoiceTel" :parent="_self" :form="form" />
          <customFormItem label="开户银行" name="invoiceBankName" :parent="_self" :form="form" />
          <customFormItem label="银行账号" name="invoiceBankNo" :parent="_self" :form="form" />
          <div id="box-list" class="box-title">厂商管理</div>
          <data-view
            propViewKey="manufacturers_crud"
            :propIsTdesign="true"
            :propParentPage="_self"
            ref="childDataView"
            :propAutoFetchData="false"
            :propTableHide="!form.id"
            propTableHideMsg="保存完主单据再维护厂商"
            @edit-input="rowEditInput"
            :propOnlyQueryAll="propReadonly"
            :propPageSize="true == propIsInfo ? 10: null"
          />
        </template>
      </customForm>
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from '@/components/custom/form/customUpload.vue'
import customImage from '@/components/custom/form/customImage.vue'

export default {
  help: '医院管理-详情',
  data() {
    return {
      form: {},
      viewData: {},
    }
  },
  methods: {
    // 加载厂商数据
    fetchChildData() {
      let queryWrapper = this.$newWrapper()
      if (this.form.id) {
        queryWrapper.eq('supplierId', this.form.id)
      } else {
        queryWrapper.ne(1, 1)
      }
      this.$refs.childDataView.setQueryWrapper(queryWrapper)
    },
    rowEditInput({row}) {
      this.$set(row, 'supplierId', this.form.id)
    },
    // 当前登录供应商信息
    getCurrentSp() {
      this.$ajax.get(`/base/suppliers/get_current_supplier`).then((response) => {
        if (response.code == 0) {
          this.form = global.utils.jsonClone(response.data)
          this.$set(this.form, 'id', this.form.id)
          console.log('this.form', this.form)
          this.fetchChildData()
        }
      })
    },

    // 保存信息
    saveInfo() {
      this.utilMessage.confirm('确认保存?', () => {
        this.utilVue.loadShow(true)
        this.$ajax.post(`/base/suppliers/saveOrUpdateCommon`, this.form).then((resp) => {
          this.utilVue.loadHide()
          if (resp.code == 0) {
            this.utilMessage.success(resp.msg)
          }
        })
      })
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        let spType = this.form.spType || []
        if (spType.includes(this.constCommon.busConst.matrTypeMatr)){
          this.$set(this.form, 'isConsumable', 1)
        }
        if (spType.includes(this.constCommon.busConst.matrTypeDrug)){
          this.$set(this.form, 'isDrug', 1)
        }
        if (spType.includes(this.constCommon.busConst.matrTypeRreagent)){
          this.$set(this.form, 'isReagent', 1)
        }
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData) => {
        this.form = global.utils.jsonClone(formData)
        this.$set(this.form, 'id', this.form.id)
        if (this.propParent.cpFormNew) {
          this.$set(this.form, 'isDrug', 0)
          this.$set(this.form, 'isConsumable', 0)
          this.$set(this.form, 'isReagent', 0)
        } else {
          let spType = []
          if (this.form.isConsumable == 1) {
            spType.push(this.constCommon.busConst.matrTypeMatr)
          }
          if (this.form.isDrug == 1) {
            spType.push(this.constCommon.busConst.matrTypeDrug)
          }
          if (this.form.isReagent == 1) {
            spType.push(this.constCommon.busConst.matrTypeRreagent)
          }
          this.$set(this.form, 'spType', spType)
        }
        this.fetchChildData()
      },
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      },
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propIsInfo: Boolean,
    propStatus: String,
    propRules: Object,
  },
  components: { customImage, customAnchor, customUpload },
  computed: {
    cpPropRules() {
      let rule = this.propRules || {}
      rule.spType = [{ required: true, message: '至少选择一种供货类型', trigger: 'blur' }]
      return rule
    }
  },
}
</script>
<style lang="scss" scoped>
.form-layout .form-right {
  max-height: calc(100% - 10px);
}
</style>
